<template>
  <div class="app-container">
    <el-table 
      style="width: 100%;" 
      v-loading="fetchLoading"
      :data="categoryList" 
      row-key="id" 
      border 
      highlight-current-row 
    >
      <el-table-column header-align="center" label="分类 ID" width="150">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="分类名称" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="分类图标" min-width="120">
        <template slot-scope="{row}">
          <el-avatar shape="square" :src="row.image ? row.image.url : ''"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.display_order }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否上线" min-width="100">
        <template slot-scope="{row}">
          <el-tag
            effect="plain"
            :type="row.status | int2BoolTag(2)"
          >{{ row.status | int2BoolTag(1) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="{row}">
          <el-button type="primary" size="small" @click="handleEdit(row.id)">编辑</el-button>
          <el-button :type="row.status | statusTag(2)" size="small" @click="handleStatus(row.id)">
            {{ row.status | statusTag(1) }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { categoryList, ChangeCateStatus } from '@/api/category'

export default {
  name: 'CategoryList',
  data() {
    return {
      categoryList: [],
      fetchLoading: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      try {
        this.fetchLoading = true
        const { data } = await categoryList()
        this.categoryList = data
      } finally {
        this.fetchLoading = false
      }
    },
    // 编辑
    handleEdit(id) {
      this.$router.push({ name: 'CategoryUpdate', params: { id } });
    },
    // 上线与下线 子级也会修改
    async handleStatus(id) {
      await ChangeCateStatus(id)
      this.$notify.success({
        title: '成功',
        message: '操作成功',
        duration: 3000,
      });
      this.getList()
    }
  },
}
</script>

<style lang="scss" scoped>

</style>